# -*- coding: cp936 -*-
#导入模块
import os
import pygame,sys,random,math
from pygame.locals import *
#窗口在屏幕中央打开
os.environ["SDL_VIDEO_CENTERED"] = "1"
#初始化函数
pygame.init()
pygame.display.set_caption("pysnake")
#设置常数
CELL_SIZE = 20  #方块大小
UP,DOWN,LEFT,RIGHT = ((0,-CELL_SIZE),(0,CELL_SIZE),
                      (-CELL_SIZE,0),(CELL_SIZE,0))
#定义game变量
game_clock = pygame.time.Clock()
game_speed = 60
game_screen_width,game_screen_height = 640,480
game_screen=pygame.display.set_mode((game_screen_width,game_screen_height)) #创建游戏窗口
game_field = game_screen.get_rect()    #rect(0,0,640,480)
game_running = True
game_playing = True
game_bgcolor = 0,0,0
game_linecolor = 33,33,33
#蛇
square_color = 255,255,255
square_color2 = 33,255,33
square_color3 = 192,192,33
square_rect = pygame.Rect(0,0,CELL_SIZE,CELL_SIZE)
food = False
square_direction = RIGHT  #蛇的初始方向
square_turn = RIGHT
square_speed = 5 #每秒走几格
square_delay = 1000 / square_speed #蛇每次运动的间隔
square_time2move = pygame.time.get_ticks() + square_delay
square_body = [pygame.Rect(0,0,0,0)]*10 #蛇的身体
food_color = 255,255,255
#游戏主循环
while game_running:
    #1用户控制，窗口点击事件
    game_screen.fill(game_bgcolor)
    if food == False:
        appleX = random.randint(0, game_screen_width / CELL_SIZE-1)
        appleY = random.randint(0, game_screen_height / CELL_SIZE-1)
        square_food = pygame.Rect(CELL_SIZE * appleX,CELL_SIZE * appleY,CELL_SIZE,CELL_SIZE)
        food = True
    pygame.draw.rect(game_screen,square_color,square_food)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False
        elif event.type == pygame.KEYDOWN:
            if square_direction in [LEFT,RIGHT]:                
                if event.key == pygame.K_UP:
                    square_turn = UP
                elif event.key == pygame.K_DOWN:
                    square_turn = DOWN
            elif square_direction in [UP,DOWN]:
                if event.key == pygame.K_LEFT:
                    square_turn = LEFT
                elif event.key == pygame.K_RIGHT:
                    square_turn = RIGHT
    #2更新数据
        #移动蛇
    if pygame.time.get_ticks() >= square_time2move:
        square_time2move = pygame.time.get_ticks() + square_delay
        square_body = [square_rect] + square_body    #增加一节身体
        square_direction = square_turn
        if  square_food == square_rect:
            square_body = [square_rect] + square_body
            food = False
        else:
            food = True
        square_body.pop()
        square_rect = square_rect.move(square_direction)#目前的方向
        #判断是否游戏结束
    if game_playing:
        #撞墙
        if not game_field.contains(square_rect):
            game_playing = False
        #撞身体
        for i in square_body:
            if square_rect == i:
                game_playing = False
        if not game_playing:
            print "GAME OVER !!!"
    #3.更新画面
    if game_playing:        
        output = "坐标:%r 速度:%r 范围:%r FPS:%0.2f 时间:%r"
        print output %(square_rect,square_direction,
                       game_field.contains(square_rect),
                       game_clock.get_fps(),pygame.time.get_ticks())
            #覆盖屏幕颜色
        #game_screen.fill(game_bgcolor)
        #画背景格子
        for i in range(CELL_SIZE,game_screen_width,CELL_SIZE):
            pygame.draw.line(game_screen,game_linecolor,
                             (i,0),(i,game_screen_height))
        for i in range(CELL_SIZE,game_screen_height,CELL_SIZE):
            pygame.draw.line(game_screen,game_linecolor,
                             (0,i),(game_screen_width,i))
            #画蛇
              #画身体
        for cell in square_body:
            game_screen.fill(square_color,cell)
            game_screen.fill(square_color2,cell.inflate(-4,-4))
              #画头
        game_screen.fill(square_color,square_rect)
        game_screen.fill(square_color3,square_rect.inflate(-4,-4))
        #画食物       
        #更新窗口内容
        font = pygame.font.Font(None,24)
        num =str(len(square_body))
        servivedtext=font.render(num,True,(255,255,255))
        game_screen.blit(servivedtext,(600,8))
    pygame.display.flip() 
    game_clock.tick(game_speed)
#游戏结束
pygame.quit()
sys.exit(0)
